package com.example.administrator.xiaobo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.bean.VarietyEntrity;

import java.util.List;

/**
 * Created by kk on 2017/2/7.
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<VarietyEntrity> datas;
    private OnChildClickListener listener;
    private RecyclerView recyclerView;


    public MyRecyclerViewAdapter(Context context, List<VarietyEntrity> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setListener(OnChildClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.variety_item, parent, false);
        view.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(view);
        return holder;

    }

    /**
     * 绑定数据
     */
    @Override
    public void onBindViewHolder(MyRecyclerViewAdapter.ViewHolder holder, int position) {
        Glide.with(context).load(datas.get(position).getLimages().getLevel1()).into(holder.img1);
        Glide.with(context).load(datas.get(position).getListicons().getLevel1()).into(holder.img3);
        imageselect(holder, position);
        holder.text1.setText(datas.get(position).getShowname());
        holder.text2.setText(datas.get(position).getShowtime());
        holder.text3.setText(datas.get(position).getDescrip());
    }

    //判断是直播还是重播
    private void imageselect(MyRecyclerViewAdapter.ViewHolder holder, int position) {
        if (datas.get(position).getShowstatus().equals("1")) {
            holder.img2.setImageResource(R.drawable.live_living);
        } else {
            holder.img2.setImageResource(R.drawable.live_has_video);
        }


    }


    @Override
    public int getItemCount() {
        return datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    //判断是tpye
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null) {
            Log.e("TAG", "onClick: "+"点击了" );
            int position = recyclerView.getChildAdapterPosition(v);
            listener.onClick(position,datas.get(position).getId(),datas.get(position).getRecordparams());
        }
    }


    /**
     * 数 据显示
     */

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img1, img2, img3;
        TextView text1, text2, text3, text4;
        RelativeLayout relativeLayout;
        private boolean isCheckde = false;

        public ViewHolder(View itemView) {
            super(itemView);
            img1 = (ImageView) itemView.findViewById(R.id.img1);
            img2 = (ImageView) itemView.findViewById(R.id.img2);
            img3 = (ImageView) itemView.findViewById(R.id.img3);
            text1 = (TextView) itemView.findViewById(R.id.text1);
            text2 = (TextView) itemView.findViewById(R.id.text2);
            text3 = (TextView) itemView.findViewById(R.id.text3);
            text4 = (TextView) itemView.findViewById(R.id.text4);
            text4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!isCheckde) {
                        text4.setText("已关注");
                        text4.setTextColor(0xff5d5d5d);
                        text4.setBackgroundResource(R.drawable.cycle2);
                        isCheckde = true;
                        Toast.makeText(context, "添加了关注", Toast.LENGTH_SHORT).show();
                    } else {
                        text4.setText("关注");
                        text4.setTextColor(0xffffda44);
                        text4.setBackgroundResource(R.drawable.cycle1);
                        isCheckde = false;
                        Toast.makeText(context, "取消了关注", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relative);

        }

    }

    //点击事件
    public interface OnChildClickListener {
        void onClick(int position, String id, String rep);
    }
}
